<?php

	$players = getAllPlayers();
	$total = new Statistics();
	foreach($players as $player) {
		$player->getAllStats();
		$player->calculateTotalStats();
	}

	function srt($a, $b) {
		if ($a->totalStats->BLindex == $b->totalStats->BLindex) return 0;
		return ($a->totalStats->BLindex > $b->totalStats->BLindex) ? -1 : 1;
	}
	usort($players, "srt");
?>

        	<div class="fullPageBox">
            	<div class="statsBox">
                <table class="tabela teamStatistics detailedStats" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <td title="Position" rowspan="2" class="statsPlayerIndex">#</td>
                        <td title="Player Name" rowspan="2" class="statsPlayerName">Player</td>
                        <td title="Team" rowspan="2">Team</td>
                        <td title="Free Throws" colspan="3" class="ui-state-default">FT</td>
                        <td title="2 Pointers" colspan="3" class="ui-state-default">2PT</td>
                        <td title="Three Pointers" colspan="3" class="ui-state-default">3PT</td>
                        <td title="Points" rowspan="2">PTS</td>
                        <td title="Assists" rowspan="2">ASS</td>
                        <td title="Defensive Rebounds" rowspan="2">DRB</td>
                        <td title="Offensive Rebounds" rowspan="2">ORB</td>
                        <td title="Blocks" rowspan="2">BLK</td>
                        <td title="Steals" rowspan="2">STL</td>
                        <td title="Fouls" rowspan="2">FLS</td>
                        <td title="BasketLiga Index" rowspan="2">IND</td>
                    </tr>
                    
                    <tr>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                    </tr>
                </thead>
                <tbody>
                
                <?php 
					$i=1;
                    foreach($players as $player) {
						$total->ft += $player->totalStats->ft;
						$total->mft += $player->totalStats->mft;
						$total->fg += $player->totalStats->fg;
						$total->mfg += $player->totalStats->mfg;
						$total->threept += $player->totalStats->threept;
						$total->mtp += $player->totalStats->mtp;
						$total->pts += $player->totalStats->pts;
						$total->ast += $player->totalStats->ast;
						$total->drbd += $player->totalStats->drbd;
						$total->orbd += $player->totalStats->orbd;
						$total->blk += $player->totalStats->blk;
						$total->stl += $player->totalStats->stl;
						$total->licne += $player->totalStats->licne;
						$total->BLindex += $player->totalStats->BLindex;
                ?>
                
                    <tr>
                        <td class="statsPlayerIndex"><?php echo $i++;?></td>
                        <td class="statsPlayerName"><a href="player/<?php echo $player->id; ?>/<?php echo $player->ime."-".$player->prezime; ?>"><?php echo $player->ime; ?> <?php echo $player->prezime; ?></a></td>
                        <td><?php echo $player->getTeamInfo()->ime; ?></td>
                        <td><?php echo $player->totalStats->ft; ?></td>
                        <td><?php echo $player->totalStats->ft+$player->totalStats->mft; ?></td>
                        <td><?php echo $player->totalStats->ft == 0 ? 0 : round(100/($player->totalStats->mft/$player->totalStats->ft + 1), 1); ?></td>
                        <td><?php echo $player->totalStats->fg; ?></td>
                        <td><?php echo $player->totalStats->fg+$player->totalStats->mfg; ?></td>
                        <td><?php echo $player->totalStats->fg == 0 ? 0 : round(100/($player->totalStats->mfg/$player->totalStats->fg + 1), 1); ?></td>
                        <td><?php echo $player->totalStats->threept; ?></td>
                        <td><?php echo $player->totalStats->threept+$player->totalStats->mtp; ?></td>
                        <td><?php echo $player->totalStats->threept == 0 ? 0 : round(100/($player->totalStats->mtp/$player->totalStats->threept + 1), 1); ?></td>
                        <td><?php echo $player->totalStats->pts; ?></td>
                        <td><?php echo $player->totalStats->ast; ?></td>
                        <td><?php echo $player->totalStats->drbd; ?></td>
                        <td><?php echo $player->totalStats->orbd; ?></td>
                        <td><?php echo $player->totalStats->blk; ?></td>
                        <td><?php echo $player->totalStats->stl; ?></td>
                        <td><?php echo $player->totalStats->licne; ?></td>
                        <td><?php echo $player->totalStats->BLindex; ?></td>
                    </tr>
                <?php 
                    }
                ?>
                </tbody>
                
                <tfoot>
                	<tr class="statsTeamTotal">
                        <td class="statsPlayerIndex"></td>
                        <td class="statsPlayerName">Total</td>
                        <td></td>
                        <td><?php echo $total->ft; ?></td>
                        <td><?php echo $total->ft+$total->mft; ?></td>
                        <td><?php echo $total->ft == 0 ? 0 : round(100/($total->mft/$total->ft + 1), 1); ?></td>
                        <td><?php echo $total->fg; ?></td>
                        <td><?php echo $total->fg+$total->mfg; ?></td>
                        <td><?php echo $total->fg == 0 ? 0 : round(100/($total->mfg/$total->fg + 1), 1); ?></td>
                        <td><?php echo $total->threept; ?></td>
                        <td><?php echo $total->threept+$total->mtp; ?></td>
                        <td><?php echo $total->threept == 0 ? 0 : round(100/($total->mtp/$total->threept + 1), 1); ?></td>
                        <td><?php echo $total->pts; ?></td>
                        <td><?php echo $total->ast; ?></td>
                        <td><?php echo $total->drbd; ?></td>
                        <td><?php echo $total->orbd; ?></td>
                        <td><?php echo $total->blk; ?></td>
                        <td><?php echo $total->stl; ?></td>
                        <td><?php echo $total->licne; ?></td>
                        <td><?php echo $total->BLindex; ?></td>
                    </tr>
                    <tr role="row">
                        <td class="ui-state-default" title="Position" rowspan="2" class="statsPlayerIndex">#</td>
                        <td title="Player Name" rowspan="2" class="statsPlayerName ui-state-default">Player</td>
                        <td title="Team" rowspan="2" class="ui-state-default">Team</td>
                        <td title="Free Throws" colspan="3" class="ui-state-default">FT</td>
                        <td title="2 Pointers" colspan="3" class="ui-state-default">2PT</td>
                        <td title="Three Pointers" colspan="3" class="ui-state-default">3PT</td>
                        <td class="ui-state-default" title="Points" rowspan="2">PTS</td>
                        <td class="ui-state-default" title="Assists" rowspan="2">ASS</td>
                        <td class="ui-state-default" title="Defensive Rebounds" rowspan="2">DRB</td>
                        <td class="ui-state-default" title="Offensive Rebounds" rowspan="2">ORB</td>
                        <td class="ui-state-default" title="Blocks" rowspan="2">BLK</td>
                        <td class="ui-state-default" title="Steals" rowspan="2">STL</td>
                        <td class="ui-state-default" title="Fouls" rowspan="2">FLS</td>
                        <td class="ui-state-default" title="BasketLiga Index" rowspan="2">IND</td>
                    </tr>
                    
                    <tr>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                    </tr>
                </tfoot>

                </table>
                </div>
            </div>

	<script type="text/javascript">             
    $(document).ready(function(){
        $('.tabela').dataTable({
			"sDom": 'R<"H"lfr>t<"F"ip>',
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"fnDrawCallback": function ( oSettings ) {
				/* Need to redo the counters if filtered or sorted */
				if ( oSettings.bSorted || oSettings.bFiltered )
				{
					this.$('td:first-child', {"filter":"applied"}).each( function (i) {
						$(this).html( i+1 );
					} );
				}
			},
			"aoColumnDefs": [
								{ "bSortable": false, "aTargets": [ 0 ] },
								{ "bSearchable": true, "aTargets": [ 1 ] },
								{ "bVisible": false, "aTargets": [ 2 ] }
							],
			"aaSorting": [[ 18, 'desc' ]],
		});
    });
    </script>